package org.nepu.sc.reflect;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.lboot.core.exception.BusinessException;
import org.nepu.sc.domain.DataVO;
import org.springframework.http.HttpStatus;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author kindear
 * 公共评价方法
 */
@Deprecated
@Slf4j
public class CommonRateUtil {
    @SneakyThrows
    public static double rate(Object obj){
        Field[] fields = obj.getClass().getDeclaredFields();
        String objName = obj.getClass().getSimpleName();
        // 计算分值
        double fz = 0.0;
        double fm = 0.0;
        for (Field field : fields) {
            field.setAccessible(true); // 允许访问私有字段
            Object value = field.get(obj);  // 获取字段值
            String name = field.getName();  // 获取字段名称
            if (field.getType() == DataVO.class){
                // 获取指定名称的无参数方法
                // 对应类为空
                if (Validator.isEmpty(value)){
                    // 抛出异常
                    throw new BusinessException(HttpStatus.BAD_REQUEST,StrUtil.format("参数[{}-{}]不可为空",objName,name));
                }else {
                    Double fzGet = ReflectUtil.invoke(value, "rate");
                    fz = fz + fzGet;
                    Double fmGet = ReflectUtil.invoke(value, "getImportant");
                    fm = fm + fmGet;
                }
            }
        }
        return fz / fm;
    }
}
